<?php
if(defined('IZ_ACCNAME')) {
require($_SESSION['IZ_CALPATH'].'includes/lib/pdf.php');
define('FPDF_FONTPATH',$_SESSION['IZ_CALPATH'].'includes/lib/');
izsql();
function init_pdf() {
    global $pdf;
    global $stdfont;
    $pdf = new PDF();
    switch($_SESSION['fontsize']) {
      case 'small':
        $pdf->SetStyle("t1","Helvetica","B",10,"0,0,0");
        $pdf->SetStyle("t2","Helvetica","B",6,"0,0,0");
        $pdf->SetStyle("t8","Helvetica","B",8,"0,0,0");
        $stdfont = 6;
        break;
      case 'medium':
        $pdf->SetStyle("t1","Helvetica","B",12,"0,0,0");
        $pdf->SetStyle("t2","Helvetica","B",8,"0,0,0");
        $pdf->SetStyle("t8","Helvetica","B",10,"0,0,0");
        $stdfont = 8;
        break;
      case 'large':
        $pdf->SetStyle("t1","Helvetica","B",16,"0,0,0");
        $pdf->SetStyle("t2","Helvetica","B",12,"0,0,0");
        $pdf->SetStyle("t8","Helvetica","B",14,"0,0,0");
        $stdfont = 12;
        break;
    }
    if(($_GET['type'] == 'year') || ($_GET['time'] == 'expanded')) {
      $pdf->FPDF('portrait');
    } else {
      $pdf->FPDF('landscape');
    }
    $pdf->AddPage();
    $pdf->SetFont('Helvetica','',$stdfont);
    $pdf->SetTextColor(0); //black
    $pdf->SetDrawColor(0);
    $pdf->SetLineWidth(.3);
}
$opt_flags = $_SESSION['IZ_OPT'];
switch($_GET['type']) {
  case 'day':
    init_pdf();
    $time = $_GET['time'];
    $pdftitle = 'iZeit - '.date($_SESSION['dateformat'],$time).'.pdf';
    $pdf->SetFont('','B',$stdfont+4);
    list($startday, $startmonth, $startyear) = explode(",",date("j,n,Y",$time));
    $end = $time+86399;
    $events = array();
    $row = array();
    $ridx = 0;
    $pdf->SetWidths(array(60,215));
    $pdf->Cell(0,10,iz_longdate($time),0,1,L,0);
    $row[$ridx]['filled'] = 'lblue';
    $row[$ridx]['data'] = '<t1>'.STR_ALLDAY.'</t1>';
    $row[$ridx]['align'] = 'R';
    $row[$ridx]['padding'] = 1;
    $celltxt = '';
    $ridx++;
    
    // ---------------------------------Begin all day events grabber ----------------------------//
    $periodstart = mktime(0,0,0,date('n',$time),date('j',$time),date('Y',$time));
    $periodend = $periodstart + 86399;
    $fromday = date('j',$time);
    $frommonth = date('n',$time);
    $fromyear = date('Y',$time);

    if($_SESSION['cpanel']) {
      $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$periodstart."' AND '".$periodend."') OR (eventend BETWEEN '".$periodstart."' AND '".$periodend."') OR (eventstart < '".$periodstart."' AND eventend > '".$periodend."' )) AND allday = 1".$_SESSION['eventcats']['filters'];
    } else {
      $eventsql = "SELECT * FROM events WHERE ((( eventstart BETWEEN '".$periodstart."' AND '".$periodend."') OR (eventend BETWEEN '".$periodstart."' AND '".$periodend."') OR (eventstart < '".$periodstart."' AND eventend > '".$periodend."' )) AND allday = 1 AND ((author = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus']."))".$_SESSION['eventcats']['filters'];
    }
    if(IZ_CALMODE == 'mysql') {
      if($_SESSION['MYSQLi']) {
        global $dblink;
        $eventqry = $dblink->query($eventsql);
          while($sqlevent = $eventqry->fetch_object()) {
            $events[] = new event($sqlevent);
          }
      } else {
        $eventqry = mysql_query($eventsql);
        if(is_resource($eventqry)) {
          while($sqlevent = mysql_fetch_object($eventqry)) {
            $events[] = new event($sqlevent);
          }
        }
      }
    } elseif(IZ_CALMODE == 'postgre') {
      $eventqry = pg_query($eventsql);
      if(is_resource($eventqry)) {
        while($sqlevent = pg_fetch_object($eventqry)) {
          $events[] = new event($sqlevent);
        }              
      }
    } elseif(IZ_CALMODE == 'msq') {
      $eventqry = mssql_query($eventsql);
      if(is_resource($eventqry)) {
        while($sqlevent = mssql_fetch_object($eventqry)) {
          $events[] = new event($sqlevent);
        }              
      }
    }
    $eventnum = count($events);
    $pdf->SetFont('','',$stdfont);
    if($eventnum > 0) {
      foreach($events as $event) {
        $celltxt .= '<t1>'.dirty($event->title).'</t1>'."\n";
        if(!empty($event->venue)) {
          $celltxt .= '<t2>'.STR_VENUE.':</t2> '.dirty($event->venue)."\n";
        }
        if(($opt_flags & OPT_CATEGORY) != 0) {
          $celltxt .= '<t2>'.STR_CATEGORY.':</t2> '.$_SESSION['eventcats'][$event->category]->name."\n";
        }
        if((!empty($event->cust1)) && ($_SESSION['cust1'] != 'none')){
          $celltxt .= ' (<t2>'.$_SESSION['cust1'].':</t2> '.$event->cust1.')'."\n";
        }
        if((!empty($event->cust2)) && ($_SESSION['cust2'] != 'none')){
          $celltxt .= ' (<t2>'.$_SESSION['cust2'].':</t2> '.$event->cust2.')'."\n";
        }
        if((!empty($event->cust3)) && ($_SESSION['cust3'] != 'none')){
          $celltxt .= ' (<t2>'.$_SESSION['cust3'].':</t2> '.$event->cust3.')'."\n";
        }
        if($_SESSION['cpanel']) {
            if(strtolower($event->author) == 'everyone') {
              $celltxt .= '<t2>'.STR_AUTHOR.':</t2> '.STR_NOTLOGGEDIN."\n";
            } else {
              $celltxt .= '<t2>'.STR_AUTHOR.':</t2> '.$event->author."\n";
            }
        }
        $celltxt .= strip_tags(dirty($event->description))."\n\n";
      }
    } else {
      $celltxt .= '<t1>'.STR_NOALLDAY.'</t1>';
    }    
    $row[$ridx]['filled'] = 'white';
    $row[$ridx]['data'] = $celltxt;
    $row[$ridx]['padding'] = 1;
    $pdf->Row($row);
    $row = array();
    $ridx = 0;
    $celltxt = '';
    if(($opt_flags & OPT_ENDDATE) != 0) {
      for($hour=0;$hour<24;$hour++) {
        $events = array();
        $houroffset = 3600*$hour;
        $hourstart = mktime($hour,0,0,$startmonth,$startday,$startyear);
        $hourend = $hourstart + 3599;
        if($_SESSION['cpanel']) {
          $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$hourstart."' AND '".$hourend."') OR (eventend BETWEEN '".$hourstart."' AND '".$hourend."') OR (eventstart < '".$hourstart."' AND eventend > '".$hourend."' )) AND allday != 1".$_SESSION['eventcats']['filters'];
        } else {
          $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$hourstart."' AND '".$hourend."') OR (eventend BETWEEN '".$hourstart."' AND '".$hourend."') OR (eventstart < '".$hourstart."' AND eventend > '".$hourend."' )) AND allday != 1 AND (author = '".strtolower($_SESSION['uname'])."'".$_SESSION['publicstatus'].")".$_SESSION['eventcats']['filters'];
        }
        if(IZ_CALMODE == 'mysql') {
          if($_SESSION['MYSQLi']) {
            global $dblink;
            $qry = $dblink->query($eventsql);      
            $numthishour = $qry->num_rows;
            if($numthishour > 0) {
              $events = array();
              while($sqlevent = $qry->fetch_object()) {
                $events[] = new event($sqlevent);
              }
            }
          } else {
            $qry = mysql_query($eventsql);      
            $numthishour = mysql_num_rows($qry);
            if($numthishour > 0) {
              $events = array();
              while($sqlevent = mysql_fetch_object($qry)) {
                $events[] = new event($sqlevent);
              }
            }
          }
        } elseif(IZ_CALMODE == 'postgre') {
          $qry = pg_query($eventsql);      
          $numthishour = pg_num_rows($qry);
          if($numthishour > 0) {
            $events = array();
            while($sqlevent = pg_fetch_object($qry)) {
              $events[] = new event($sqlevent);
            }
          }
        } elseif(IZ_CALMODE == 'msq') {
          $qry = mssql_query($eventsql);      
          $numthishour = mssql_num_rows($qry);
          if($numthishour > 0) {
            $events = array();
            while($sqlevent = mssql_fetch_object($qry)) {
              $events[] = new event($sqlevent);
            }
          }
        }
        if($hour<10) {
          $celltxt .= '<t1>0'.$hour.':00</t1>';
        } else {
          $celltxt .= '<t1>'.$hour.':00</t1>';
        }
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = $celltxt;
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        foreach($events as $event) {
          $celltxt .= '<t1>'.dirty($event->title);
          if($event->eventstart == $event->eventend) {
            $celltxt .= ' ('.date("H:i",$event->eventstart).')</t1>'."\n";
          } else {
            $celltxt .= ' ('.date("H:i",$event->eventstart).' - '.date("H:i",$event->eventend).')</t1>'."\n";
          }
          if(($opt_flags & OPT_VENUE) != 0 && !empty($event->venue)) {
            $celltxt .= '<t2>'.STR_VENUE.':</t2> '.dirty($event->venue)."\n";
          }
          if(($opt_flags & OPT_ATTENDEES) != 0 && !empty($event->attendees)) {
            $celltxt .= '<t2>'.STR_ATTENDEES.':</t2> '.commasplit($event->attendees)."\n";
          }
          if(($event->cust1 != '') && ($_SESSION['cust1'] != 'none')){
            $celltxt .= '<t2>'.$_SESSION['cust1'].':</t2> '.dirty($event->cust1)."\n";
          }
          if(($event->cust2 != '') && ($_SESSION['cust2'] != 'none')){
            $celltxt .= '<t2>'.$_SESSION['cust2'].':</t2> '.dirty($event->cust2)."\n";
          }
          if(($event->cust3 != '') && ($_SESSION['cust3'] != 'none')){
            $celltxt .= '<t2>'.$_SESSION['cust3'].':</t2> '.dirty($event->cust3)."\n";
          }
          if($_SESSION['cpanel']) {
            if(strtolower($event->author) == 'everyone') {
              $celltxt .= '<t2>'.STR_AUTHOR.':</t2> '.STR_NOTLOGGEDIN."\n";
            } else {
              $celltxt .= '<t2>'.STR_AUTHOR.':</t2> '.$event->author."\n";
            }
          }
          if(($opt_flags & OPT_CATEGORY) != 0) {
            $celltxt .= '<t2>'.STR_CATEGORY.':</t2> '.$_SESSION['eventcats'][$event->category]->name."\n";
          }
          $celltxt .= strip_tags(dirty($event->description))."\n\n";
        }
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = $celltxt;
        $row[$ridx]['padding'] = 1;
        $pdf->Row($row);
        $ridx = 0;
        $celltxt = '';
        $row = array();
      }
    }
//    $pdf->footer();
    $pdf->Output($pdftitle,'I');
    break;
  case 'week':
    init_pdf();
    $time = $_GET['time'];
    $pdftitle = 'iZeit - '.STR_WEEK.' '.date($_SESSION['dateformat'],$time).'.pdf';
    $pdf->SetFont('','B',$stdfont+4);
    
    $pdf->SetWidths(array(80,200));
    $pdf->Cell(0,10,STR_WEEKSTARTING.' '.iz_longdate($time),0,1,L,0);
    $pdf->SetFont('','',$stdfont);
    $firstdaynum = date('j',$time) - getOffsetDisplayedIndex(date('w',$time));
    $month = date('n',$time);
    $year = date('Y',$time);
    $firstdaysecs = mktime(0,0,0,date('n',$time),$firstdaynum,date('Y',$time));
    $endweeksecs = mktime(23,59,59,date('n',$time),$firstdaynum+6,date('Y',$time));
    for($daynum=0;$daynum<7;$daynum++) {
      $celltxt = '<t1>';
      $row = array();
      $ridx = 0;
      $daystart = $firstdaysecs + ($daynum*86400);
      $dayend = $daystart + 86399;
      $celltxt .= $daynames[getLocalizedIndex($daynum)].' ';
      if($_SESSION['IZ_LANG'] == 'en') {
        $celltxt .= date("jS",$daystart);
      } else {
        $celltxt .= date('j',$daystart).' '.$monthnames[date('n',$daystart)];
      }
      $celltxt .= '</t1>';
      $row[$ridx]['filled'] = 'lblue';
      $row[$ridx]['data'] = $celltxt;
      $row[$ridx]['align'] = 'R';
      $row[$ridx]['padding'] = 1;
      $ridx++;
      $celltxt = '';
      $events = array();
      $alldayevents = array();
      if($_SESSION['cpanel']) {
        $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$daystart."' AND '".$dayend."') OR (eventend BETWEEN '".$daystart."' AND '".$dayend."') OR (eventstart < '".$daystart."' AND eventend > '".$dayend."' ))".$_SESSION['eventcats']['filters'];
      } else {
        $eventsql = "SELECT * FROM events WHERE ((( eventstart BETWEEN '".$daystart."' AND '".$dayend."') OR (eventend BETWEEN '".$daystart."' AND '".$dayend."') OR (eventstart < '".$daystart."' AND eventend > '".$dayend."' )) AND ((author = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus']."))".$_SESSION['eventcats']['filters'];
      }
      if(IZ_CALMODE == 'mysql') {
        if($_SESSION['MYSQLi']) {
          global $dblink;
          $eventqry = $dblink->query($eventsql);
            while($sqlevent = $eventqry->fetch_object()) {
              $event = new event($sqlevent);
              if($event->allday) {
                $alldayevents[] = $event;
              } else {
                $events[] = $event;
              }
            }
        } else {
          $eventqry = mysql_query($eventsql);
          if(is_resource($eventqry)) {
            while($sqlevent = mysql_fetch_object($eventqry)) {
              $event = new event($sqlevent);
              if($event->allday) {
                $alldayevents[] = $event;
              } else {
                $events[] = $event;
              }
            }
          }
        }
      } elseif(IZ_CALMODE == 'postgre') {
        $eventqry = pg_query($eventsql);
        if(is_resource($eventqry)) {
          while($sqlevent = pg_fetch_object($eventqry)) {
            $event = new event($sqlevent);
            if($event->allday) {
              $alldayevents[] = $event;
            } else {
              $events[] = $event;
            }
          }           
        }
      } elseif(IZ_CALMODE == 'msq') {
        $eventqry = mssql_query($eventsql);
        if(is_resource($eventqry)) {
          while($sqlevent = mssql_fetch_object($eventqry)) {
            $event = new event($sqlevent);
            if($event->allday) {
              $alldayevents[] = $event;
            } else {
              $events[] = $event;
            }
          }           
        } 
      }
      if(empty($events) && empty($alldayevents)) {
        $celltxt .= '<t1>'.STR_NOEVENTSPRINT.'</t1>';
      } else {
        if(!empty($alldayevents)) {
          foreach($alldayevents as $alldayevent) {
            $celltxt .= '<t1>'.dirty($alldayevent->title).'</t1> ('.STR_ALLDAYSHORT.')'."\n";
            if(($opt_flags & OPT_CATEGORY) != 0) {
              $celltxt .= '<t2>'.STR_CATEGORY.':</t2> '.$_SESSION['eventcats'][$alldayevent->category]->name."\n";
            }
            if(!empty($alldayevent->venue)) {
              $celltxt .= '<t2>'.STR_VENUE.':</t2> '.dirty($alldayevent->venue)."\n";
            }
            if(($alldayevent->cust1 != '') && ($_SESSION['cust1'] != 'none')){
              $celltxt .= '<t2>'.$_SESSION['cust1'].':</t2> '.dirty($alldayevent->cust1)."\n";
            }
            if(($alldayevent->cust2 != '') && ($_SESSION['cust2'] != 'none')){
              $celltxt .= '<t2>'.$_SESSION['cust2'].':</t2> '.dirty($alldayevent->cust2)."\n";
            }
            if(($alldayevent->cust3 != '') && ($_SESSION['cust3'] != 'none')){
              $celltxt .= '<t2>'.$_SESSION['cust3'].':</t2> '.dirty($alldayevent->cust3)."\n";
            }
            if($_SESSION['cpanel']) {
              if(strtolower($alldayevent->author) == 'everyone') {
                $celltxt .= '<t2>'.STR_AUTHOR.':</t2> '.STR_NOTLOGGEDIN."\n";
              } else {
                $celltxt .= '<t2>'.STR_AUTHOR.':</t2> '.$alldayevent->author."\n";
              }
            }
            $celltxt .= strip_tags(dirty($alldayevent->description))."\n\n";
          }
        }
        if(!empty($events)) {
          for($hour=0;$hour<24;) {
            $hourstart = $daystart + ($hour*3600);
            $hourend = $hourstart + 3600;
            $hourevents = array();                                               
            foreach($events as $event) { 
              if(($event->eventstart >= $hourstart) && ($event->eventstart < $hourend)) {
                $hourevents[] = $event;
              }
            }
            if(!empty($hourevents)) {
              $hashourly = 1;
              foreach($hourevents as $hourevent) {  
                $duration = calcduration($hourevent->eventstart,$hourevent->eventend);
                $celltxt .= '<t1>'.dirty($hourevent->title).'</t1>'."\n".
                    '<t2>'.STR_CATEGORY.':</t2> '.$_SESSION['eventcats'][$hourevent->category]->name."\n".
                    '<t2>'.STR_STARTS.':</t2> '.date("H:i",$hourevent->eventstart)."\n";
                if($duration != '0h 0m') {
                  $celltxt .= '<t2>'.STR_DURATION.':</t2> '.$duration."\n";
                }
                if(!empty($hourevent->venue)) {
                  $celltxt .= '<t2>'.STR_VENUE.':</t2> '.dirty($hourevent->venue)."\n";
                }
                if(($hourevent->cust1 != '') && ($_SESSION['cust1'] != 'none')) {
                  $celltxt .= '<t2>'.$_SESSION['cust1'].':</t2> '.dirty($hourevent->cust1)."\n";
                }
                if(($hourevent->cust2 != '') && ($_SESSION['cust2'] != 'none')) {
                  $celltxt .= '<t2>'.$_SESSION['cust2'].':</t2> '.dirty($hourevent->cust2)."\n";
                }
                if(($hourevent->cust3 != '') && ($_SESSION['cust3'] != 'none')) {
                  $celltxt .= '<t2>'.$_SESSION['cust3'].':</t2> '.dirty($hourevent->cust3)."\n";
                }
                if($_SESSION['cpanel']) {
                  if(strtolower($hourevent->author) == 'everyone') {
                    $celltxt .= '<t2>'.STR_AUTHOR.':</t2> '.STR_NOTLOGGEDIN."\n";
                  } else {
                    $celltxt .= '<t2>'.STR_AUTHOR.':</t2> '.$hourevent->author."\n";
                  }
                }
                $celltxt .= strip_tags(dirty($hourevent->description))."\n\n";
              }
            } 
            $hour++;
          }
        }
      }
      $row[$ridx]['filled'] = 'white';
      $row[$ridx]['data'] = $celltxt;
      $row[$ridx]['padding'] = 1;
      $pdf->Row($row);
      $ridx = 0;
    }
    $pdf->Output($pdftitle,'I');
    break;
  case 'month':
  case 'extmonth':
    init_pdf();
    $time = $_GET['time'];
    $cellwidth = 39;
    $pdf->SetWidths(array($cellwidth,$cellwidth,$cellwidth,$cellwidth,$cellwidth,$cellwidth,$cellwidth));
    $month = date('n',$time);
    $year = date('Y',$time);
    $pdftitle = 'iZeit - '.$monthnames[date('n',$time)].' '.$year.'.pdf';
    $firstdayofmonth = mktime(0,0,0,$month,1,$year);
    $lastdayofmonth = mktime(0,0,0,$month+1,1,$year) - 86400;
    $numdays = date('t',$firstdayofmonth);
    $monthinfo = getdate($firstdayofmonth);
    $lastdayinfo = getdate($lastdayofmonth);
    $colcount = 0;
    $ridx = 0;
    $firstvisiblecol = getOffsetDisplayedIndex(date('w',$firstdayofmonth));
    $lastvisiblecol = getOffsetDisplayedIndex(date('w',$lastdayofmonth));
    $monthname = $monthnames[date('n',$firstdayofmonth)];

    $pdf->SetFillcolor(204,223,235);
    $pdf->SetFont('','B',$stdfont+4);
    $pdf->Cell(0,10,$monthname.' '.$year,0,1,L,0);

    $pdf->SetFont('','',$stdfont);
    $ridx = 0;
    for($i=0;$i<7;$i++) {
      $name = $daynames[getLocalizedIndex($i)];
      $row[$ridx]['filled'] = 'lblue';
      $row[$ridx]['padding'] = 2;
      $row[$ridx]['align'] = 'C';
      $row[$ridx]['data'] = '<t8>'.$name.'</t8>';
      $ridx++;
    }
    $pdf->Row($row);
    $row = array();
    $ridx = 0;
    
    $row = array();
    for($i=0;$i<$firstvisiblecol;$i++) {
      $row[$ridx]['filled'] = 'grey';
      $row[$ridx]['data'] = '';
      $colcount++;
      $ridx++;
    }
    for($loopday = 0; $loopday<$numdays; $loopday++) {
        $events = array();
        $periodstart = $firstdayofmonth+($loopday*86400);
        $periodend = $periodstart+86399;
        if($_SESSION['cpanel']) {
          $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$periodstart."' AND '".$periodend."') OR (eventend BETWEEN '".$periodstart."' AND '".$periodend."') OR (eventstart < '".$periodstart."' AND eventend > '".$periodend."' ))".$_SESSION['eventcats']['filters'];
        } else {
          $eventsql = "SELECT * FROM events WHERE ((( eventstart BETWEEN '".$periodstart."' AND '".$periodend."') OR (eventend BETWEEN '".$periodstart."' AND '".$periodend."') OR (eventstart < '".$periodstart."' AND eventend > '".$periodend."' )) AND ((author = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus']."))".$_SESSION['eventcats']['filters'];
        }
        if(IZ_CALMODE == 'mysql') {
          if($_SESSION['MYSQLi']) {
            global $dblink;
            $eventqry = $dblink->query($eventsql);
              while($sqlevent = $eventqry->fetch_object()) {
                $events[] = new event($sqlevent);
              }
          } else {
            $eventqry = mysql_query($eventsql);
            if(is_resource($eventqry)) {
              while($sqlevent = mysql_fetch_object($eventqry)) {
                $events[] = new event($sqlevent);
              }
            }
          }
        } elseif(IZ_CALMODE == 'postgre') {
          $eventqry = pg_query($eventsql);
          if(is_resource($eventqry)) {
            while($sqlevent = pg_fetch_object($eventqry)) {
              $events[] = new event($sqlevent);
            }
          }
        } elseif(IZ_CALMODE == 'msq') {
          $eventqry = mssql_query($eventsql);
          if(is_resource($eventqry)) {
            while($sqlevent = mssql_fetch_object($eventqry)) {
              $events[] = new event($sqlevent);
            }
          }
        }
        $eventnum = count($events);
        if($eventnum > 0) {
          $celltxt = '<t1>'.($loopday+1).'</t1>'."\n";
          foreach($events as $event) {
//echo $loopday.'='.date('jS F Y',$periodstart).'-'.date('jS F Y',$event->eventstart);
            $duration = calcduration($event->eventstart,$event->eventend);
            $celltxt .= '<t8>'.dirty($event->title).'</t8>'."\n";
            if(($opt_flags & OPT_ENDDATE) != 0) {
              if($event->allday != 1) { 
                $celltxt .= '<t2>'.STR_STARTS.':</t2> '.date("H:i",$event->eventstart)."\n";
              }
              if($duration != '0m') {
                $celltxt .= '<t2>'.STR_DURATION.':</t2> '.$duration."\n";                
              }
            }
          }
          $row[$ridx]['filled'] = 'orange';
          $row[$ridx]['data'] = $celltxt;
          $ridx++;
        } else {
          $row[$ridx]['filled'] = 'white';
          $row[$ridx]['data'] = '<t1>'.($loopday+1).'</t1>';
          $ridx++;
        }
        if(($colcount == 6) && ($loopday < 31)) {
          $pdf->Row($row);
          $colcount = 0;
          $row = array();
          $ridx = 0;
        } else {
          $colcount++;
        }
    }
    for($i=$lastvisiblecol;$i<6;$i++) {
        $row[$ridx]['filled'] = 'grey';
        $row[$ridx]['data'] = '';
        $ridx++;
    }
    $pdf->Row($row);
    $pdf->Output($pdftitle,'I');
    break;
  case 'year':
    init_pdf();
    $time = $_GET['time'];
    $year = date('Y',$time);
    
    $pdftitle = 'iZeit - '.$year.'.pdf';
    $rowwidth = 55;
    $colheight = 60;
    $rownum = 0;
    $colnum = 0;
    $cellwidth = $rowwidth / 7;
    $pdf->SetWidths(array($cellwidth,$cellwidth,$cellwidth,$cellwidth,$cellwidth,$cellwidth,$cellwidth));
    $pdf->SetFillcolor(204,223,235);
    $pdf->SetFont('','B',$stdfont+5);
    $pdf->Cell(0,10,date('Y',$time),0,1,C,0);
    
    $pdf->SetFont('','',$stdfont);
    $startx = $pdf->GetX();
    $starty = $pdf->GetY();
    for($month=1;$month<=12;$month++) {
      $firstdayofmonth = mktime(0,0,0,$month,1,$year);
      $lastdayofmonth = mktime(0,0,0,$month+1,1,$year) - 86400;
      $firstvisiblecol = getOffsetDisplayedIndex(date('w',$firstdayofmonth));
      $lastvisiblecol = getOffsetDisplayedIndex(date('w',$lastdayofmonth));
      $numdays = date('t',$firstdayofmonth);
      $monthinfo = getdate($firstdayofmonth);
      $lastdayinfo = getdate($lastdayofmonth);
      $colcount = 0;
      $monthname = $monthnames[date('n',$firstdayofmonth)];
      $xoffset = $startx+($rownum*$rowwidth)+($cellwidth*$rownum);
      $yoffset = $starty+($colnum*$colheight);
      $pdf->SetXY($xoffset,$yoffset);
      $pdf->SetFont('','B',$stdfont+2);
      $pdf->SetFillcolor(204,223,235);
      $pdf->Cell($rowwidth,10,$monthname,1,1,C,1);
      $pdf->SetX($xoffset);
      $pdf->SetFont('','',$stdfont);
      $ridx = 0;
      $row = array();
      $pdf->SetFont('','B');
      for($i=0;$i<7;$i++) {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['align'] = 'C';
        if($_SESSION['fontsize'] == 'large') {
          $row[$ridx]['data'] = $daynames[getLocalizedIndex($i)]{0};
        } else {
          $row[$ridx]['data'] = $weekdays[getLocalizedIndex($i)];
        }
        $ridx++;
      }
      $pdf->SetFont('','');
      $pdf->Row($row);
      $row = array();
      $ridx = 0;     

      for($i=0;$i<$firstvisiblecol;$i++) {
        $row[$ridx]['filled'] = 'grey';
        $row[$ridx]['data'] = '';
        $colcount++;
        $ridx++;
      } 
      for($loopday = 0; $loopday<$numdays; $loopday++) {
        $events = array();
        $periodstart = $firstdayofmonth+($loopday*86400);
        $periodend = $periodstart+86399;
        $eventnum = get_num_visible($periodstart,$periodend);
        $row[$ridx]['align'] = 'C';
        if($eventnum > 0) {
          $celltxt = ($loopday+1);
          $row[$ridx]['filled'] = 'orange';
          $row[$ridx]['data'] = $celltxt;
          $ridx++;
        } else {
          $row[$ridx]['filled'] = 'white';
          $row[$ridx]['data'] = ($loopday+1);
          $ridx++;
        }
        if(($colcount == 6) && ($loopday < 31)) {
          $pdf->SetX($xoffset);
          $pdf->Row($row);
          $colcount = 0;
          $row = array();
          $ridx = 0;
        } else {
          $colcount++;
        }
      }
      for($i=$lastvisiblecol;$i<6;$i++) {
        $row[$ridx]['filled'] = 'grey';
        $row[$ridx]['data'] = ' ';
        $ridx++;
      }
      $pdf->SetX($xoffset);
      if(!empty($row)) { 
        $pdf->Row($row); 
      }
      if($rownum == 0) {
        $rownum++;
      } elseif(($rownum % 2) == 0) {
        $colnum++;
        $rownum = 0;
      } else {
        $rownum++;
      }
    }
    $pdf->Output($pdftitle,'I');
    break;
  case "category":
    $events = array();
    $todos = array();
    $catid = $_GET['time'];
    izsql();
    if($_SESSION['cpanel']) {
      $eventsql = "SELECT * FROM events WHERE category = '".$catid."' ORDER BY title ASC, startdate ASC";
      $todosql = "SELECT * FROM tasks WHERE category = '".$catid."' ORDER BY title ASC, due ASC";
    } else {
      $eventsql = "SELECT * FROM events WHERE category = '".$catid."' AND ((LOWER(author) = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].") ORDER BY title ASC, eventstart ASC";
      $todosql = "SELECT * FROM tasks WHERE category = '".$catid."' AND ((LOWER(author) = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].") ORDER BY title ASC, due ASC";
    }
    if(IZ_CALMODE == 'mysql') {
      if($_SESSION['MYSQLi']) {
        global $dblink;
        $eventqry = $dblink->query($eventsql);
        if($eventqry->num_rows > 0) {
          while($sqlevent = $eventqry->fetch_object()) {
            $events[] = new event($sqlevent);
          }
        }
        if(($opt_flags & OPT_TODO) != 0) {
          $todoqry = $dblink->query($todosql);
          if($todoqry->num_rows > 0) {
            while($sqlres = $todoqry->fetch_object()) {
              $todos[] = new todo($sqlres);
            }
          }
        }
      } else {
        $eventqry = mysql_query($eventsql);
        if((is_resource($eventqry)) && (mysql_num_rows($eventqry) > 0)) {
          while($sqlevent = mysql_fetch_object($eventqry)) {
            $events[] = new event($sqlevent);
          }
        }
        if(($opt_flags & OPT_TODO) != 0) {
          $todoqry = mysql_query($todosql);
          if((is_resource($todoqry)) && (mysql_num_rows($todoqry) > 0)) {
            while($sqlres = mysql_fetch_object($todoqry)) {
              $todos[] = new todo($sqlres);
            }
          }
        }
      }
    } elseif(IZ_CALMODE == 'postgre') {
      $eventqry = pg_query($eventsql);
      if((is_resource($eventqry)) && (pg_num_rows($eventqry) > 0)) {
        while($sqlevent = pg_fetch_object($eventqry)) {
          $events[] = new event($sqlevent);
        }
      }
      if(($opt_flags & OPT_TODO) != 0) {
        $todoqry = pg_query($todosql);
        if((is_resource($todoqry)) && (pg_num_rows($todoqry) > 0)) {
          while($sqlres = pg_fetch_object($todoqry)) {
            $todos[] = new todo($sqlres);
          }
        }
      }
    } elseif(IZ_CALMODE == 'msq') {
      $eventqry = mssql_query($eventsql);
      if((is_resource($eventqry)) && (mssql_num_rows($eventqry) > 0)) {
        while($sqlevent = mssql_fetch_object($eventqry)) {
          $events[] = new event($sqlevent);
        }
      }
      if(($opt_flags & OPT_TODO) != 0) {
        $todoqry = mssql_query($todosql);
        if((is_resource($todoqry)) && (mssql_num_rows($todoqry) > 0)) {
          while($sqlres = mssql_fetch_object($todoqry)) {
            $todos[] = new todo($sqlres);
          }
        }
      }
    }
    if(($_SESSION['canview'] != 1) || ($_SESSION['cpanel'] != 1)) {
      iz_error('#200: '.ERR_NOPERMISSION);
    } else {
      init_pdf();
      $pdf->SetFillcolor(204,223,235);
      $row = array();
      $ridx = 0;
      if(!empty($events)) {
        $pdf->SetFont('','B',$stdfont+4);
        $pdftitle = 'iZeit - '.$_SESSION['eventcats'][$catid]->name.'.pdf';
        $pdf->Cell(0,10,STR_VIEWINGCATEGORY.': '.$_SESSION['eventcats'][$catid]->name,0,1,C,0);
        $pdf->SetFont('','',$stdfont);
        if($time == 'expanded') {
          $pdf->SetWidths(array(60,215));
          foreach($events as $event) {
            $pdf->SetFont('','B',$stdfont+2);
            $pdf->SetFillcolor(204,223,235);
            $pdf->Cell(275,$stdfont,dirty($event->title),1,1,L,1);
            $pdf->SetFont('','',$stdfont);
            if(($opt_flags & OPT_ENDDATE) != 0) {
              if($event->allday) {
//TODO: Add info 'i' image!!!
                $row[0]['filled'] = 'lblue';
                $row[0]['data'] = '<t2>'.STR_STARTS.'</t2>';
                $row[0]['align'] = 'R';
                $row[1]['filled'] = 'white';
                $row[1]['data'] = iz_longdate($event->eventstart).' ('.STR_ALLDAYDESC.')';
                $pdf->Row($row);
                $row = array();
              } else {
                $row[0]['filled'] = 'lblue';
                $row[0]['data'] = '<t2>'.STR_STARTS.'</t2>';
                $row[0]['align'] = 'R';
                $row[1]['filled'] = 'white';
                $row[1]['data'] = iz_longdatehours($event->eventstart);
                $pdf->Row($row);
                $row = array();

                $row[0]['filled'] = 'lblue';
                $row[0]['data'] = '<t2>'.STR_ENDS.'</t2>';
                $row[0]['align'] = 'R';
                $row[1]['filled'] = 'white';
                $row[1]['data'] = iz_longdatehours($event->eventend);
                $pdf->Row($row);
                $row = array();
              }
            } else {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.STR_DATE.'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = iz_longdatehours($event->eventstart);
              $pdf->Row($row);
              $row = array();
            }
            if(($opt_flags & OPT_VENUE) != 0) {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.STR_VENUE.'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($event->venue);
              $pdf->Row($row);
              $row = array();
            }                     
            if($_SESSION['cust1'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust1'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($event->cust1);
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust2'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust2'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($event->cust2);
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust3'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust3'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($event->cust3);
              $pdf->Row($row);
              $row = array();
            }
            $row[0]['filled'] = 'lblue';
            $row[0]['data'] = '<t2>'.STR_DESCRIPTION.'</t2>';
            $row[0]['align'] = 'R';
            $row[1]['filled'] = 'white';
            $row[1]['data'] = strip_tags(dirty($event->description));
            $pdf->Row($row);
            $row = array();
          
            $pdf->Ln();
          }
        } else {
          $colsize['name'] = 55;
          if(($opt_flags & OPT_ENDDATE) != 0) { $colsize['start'] = 25; $colsize['end'] = 25; $colsize['duration'] = 25; } else { $colsize['start'] = 25; }
          if(($opt_flags & OPT_VENUE) != 0) { $colsize['venue'] = 45; }
          if($_SESSION['cust1'] != 'none') { $colsize['cust1'] = 40; }
          if($_SESSION['cust2'] != 'none') { $colsize['cust2'] = 40; }
          if($_SESSION['cust3'] != 'none') { $colsize['cust3'] = 40; }
          $pdf->SetWidths($colsize);
         
          $row['name']['data'] = '<t1>'.STR_NAME.'</t1>';
          if(($opt_flags & OPT_ENDDATE) != 0) {
            $row['start']['data'] = '<t1>'.STR_STARTS.'</t1>';
            $row['end']['data'] = '<t1>'.STR_ENDS.'</t1>';
            $row['duration']['data'] = '<t1>'.STR_DURATION.'</t1>';
          } else {
            $row['start']['data'] = '<t1>'.STR_DATE.'</t1>';
          }
          if(($opt_flags & OPT_VENUE) != 0) {
            $row['venue']['data'] = '<t1>'.STR_VENUE.'</t1>';
          }
          if($_SESSION['cust1'] != 'none') {
            $row['cust1']['data'] = '<t1>'.$_SESSION['cust1'].'</t1>';
          }
          if($_SESSION['cust2'] != 'none') {
            $row['cust2']['data'] = '<t1>'.$_SESSION['cust2'].'</t1>';
          }
          if($_SESSION['cust3'] != 'none') {
            $row['cust3']['data'] = '<t1>'.$_SESSION['cust3'].'</t1>';
          }
          $row['meta']['filled'] = 'lblue';
          $row['meta']['padding'] = 1;
          $pdf->Row_assoc($row);
          $row = array();

          $rc = 1;                  
          foreach($events as $event) {
            if(is_int($rc/2)) { 
              $row['meta']['filled'] = 'white';
            } else { 
              $row['meta']['filled'] = '207';
            }
            $rc++;
            $row['name']['data'] = dirty($event->title);
            if(($opt_flags & OPT_ENDDATE) != 0) {
              $row['start']['data'] = date($_SESSION['dateformat'],$event->eventstart);
              $row['end']['data'] = date($_SESSION['dateformat'],$event->eventend);
              if($event->allday) {
                   $row['duration']['data'] = STR_ALLDAYSHORT;
              } else {
                   $row['duration']['data'] = calcduration($event->eventstart,$event->eventend);
              }
            } else {
              $row['start']['data'] = date($_SESSION['dateformat'],$event->eventstart);
            }
            if(($opt_flags & OPT_VENUE) != 0) {
              $row['venue']['data'] = dirty($event->venue);
            }
            if($_SESSION['cust1'] != 'none') {
              $row['cust1']['data'] = dirty($event->cust1);
            }
            if($_SESSION['cust2'] != 'none') {
              $row['cust2']['data'] = dirty($event->cust2);
            }
            if($_SESSION['cust3'] != 'none') {
              $row['cust3']['data'] = dirty($event->cust3);
            }
            $pdf->Row_assoc($row);
            $row = array();
          }
        }
      }
      if(!empty($todos)) {
        $pdf->SetFont('','B',$stdfont+4);
        $pdftitle = 'iZeit - '.$_SESSION['todocats'][$catid]->name.'.pdf';
        $pdf->Cell(0,10,STR_VIEWINGCATEGORY.': '.$_SESSION['todocats'][$catid]->name,0,1,C,0);
        $pdf->SetFont('','',$stdfont);
        if($time == 'expanded') {
          $pdf->SetWidths(array(60,215));
          foreach($todos as $todo) {
            $pdf->SetFont('','B',$stdfont+2);
            $pdf->SetFillcolor(204,223,235);
            $pdf->Cell(275,$stdfont,dirty($todo->title),1,1,L,1);
            $pdf->SetFont('','',$stdfont);
//TODO: Add info 'i' image!!!
            $row[0]['filled'] = 'lblue';
            $row[0]['data'] = '<t2>'.STR_DATEDUE.'</t2>';
            $row[0]['align'] = 'R';
            $row[1]['filled'] = 'white';
            if($todo->due == 0) {
              $row[1]['data'] = STR_ONGOING;
            } else {
              if($todo->complete) {
                $row[1]['data'] = iz_longdate($todo->due).' ('.STR_COMPLETED.')';
              } else {
                $row[1]['data'] = iz_longdate($todo->due);
              }
            }
            $pdf->Row($row);
            $row = array();                  
            if($_SESSION['cust1'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust1'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($todo->cust1);
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust2'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust2'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($todo->cust2);
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust3'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust3'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($todo->cust3);
              $pdf->Row($row);
              $row = array();
            }
            $row[0]['filled'] = 'lblue';
            $row[0]['data'] = '<t2>'.STR_DESCRIPTION.'</t2>';
            $row[0]['align'] = 'R';
            $row[1]['filled'] = 'white';
            $row[1]['data'] = strip_tags(dirty($todo->description));
            $pdf->Row($row);
            $row = array();
          
            $pdf->Ln();
          }
        } else {
          $colsize = array('name' => 50,
                           'desc' => 80,
                           'due' => 40
                          );
          if($_SESSION['cust1'] != 'none') { $colsize['cust1'] = 30; }
          if($_SESSION['cust2'] != 'none') { $colsize['cust2'] = 30; }
          if($_SESSION['cust3'] != 'none') { $colsize['cust3'] = 30; }
          $pdf->SetWidths($colsize);
          $row['name']['data'] = '<t1>'.STR_TASKNAME.'</t1>';
          $row['desc']['data'] = '<t1>'.STR_DESCRIPTION.'</t1>';
          $row['due']['data'] = '<t1>'.STR_DATEDUE.'</t1>';
          if($_SESSION['cust1'] != 'none') {
            $row['cust1']['data'] = '<t1>'.$_SESSION['cust1'].'</t1>';
          }
          if($_SESSION['cust2'] != 'none') {
            $row['cust2']['data'] = '<t1>'.$_SESSION['cust2'].'</t1>';
          }
          if($_SESSION['cust3'] != 'none') {
            $row['cust3']['data'] = '<t1>'.$_SESSION['cust3'].'</t1>';
          }
          $row['meta']['filled'] = 'lblue';
          $row['meta']['padding'] = 1;
          $pdf->Row_assoc($row);
          $row = array();
          $rc = 1;                  
          foreach($todos as $todo) {
            if(is_int($rc/2)) { 
              $row['meta']['filled'] = 'white';
            } else { 
              $row['meta']['filled'] = '207';
            }
            $rc++;
            $row['name']['data'] = dirty($todo->title);
           if(strlen($todo->description) > 20) {
              $row['desc']['data'] = substr(nobreaks(dirty($todo->description)),0,20).'...';
            } else {
              $row['desc']['data'] = strip_tags(nobreaks($todo->description));
            }
            if($todo->due == 0) {
              $row['due']['data'] = STR_ONGOING;
            } else {
              if($todo->complete) {
                $row['due']['data'] = date($_SESSION['dateformat'],$todo->due).' ('.STR_COMPLETE.')';
              } else {
                $row['due']['data'] = date($_SESSION['dateformat'],$todo->due);
              }
            }
            if($_SESSION['cust1'] != 'none') {
              $row['cust1']['data'] = dirty($todo->cust1);
            }
            if($_SESSION['cust2'] != 'none') {
              $row['cust2']['data'] = dirty($todo->cust2);
            }
            if($_SESSION['cust3'] != 'none') {
              $row['cust3']['data'] = dirty($todo->cust3);
            }
            $pdf->Row_assoc($row);
            $row = array();
          }
        }
      }
//      $pdf->Output($pdftitle,'I');
      $pdf->Output();
    }
    break;
  case "listview":
    $events = array();
    $todos = array();
    izsql();
    if($_SESSION['cpanel']) {
      $eventsql = "SELECT * FROM events WHERE 1".$_SESSION['eventcats']['filters']." ORDER BY eventstart";
      $todosql = "SELECT * FROM tasks WHERE 1".$_SESSION['todocats']['filters']." AND due < '".$_SESSION['todospan']['value']."' AND (complete = '0' OR complete = '".$_SESSION['completedtodos']."') ORDER BY due ASC";
    } else {
      $eventsql = "SELECT * FROM events WHERE ((LOWER(author) = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].")".$_SESSION['eventcats']['filters']." ORDER BY eventstart";
      $todosql = "SELECT * FROM tasks WHERE ((LOWER(author) = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].")  AND due < '".$_SESSION['todospan']['value']."' AND (complete = '0' OR complete = '".$_SESSION['completedtodos']."')".$_SESSION['todocats']['filters']." ORDER BY due ASC";
    }
    if(IZ_CALMODE == 'mysql') {
      if($_SESSION['MYSQLi']) {
        global $dblink;
        $eventqry = $dblink->query($eventsql);
        if($eventqry->num_rows > 0) {
          while($sqlevent = $eventqry->fetch_object()) {
            $events[] = new event($sqlevent);
          }
        }
        if(($opt_flags & OPT_TODO) != 0) {
          $todoqry = $dblink->query($todosql);
          if($todoqry->num_rows > 0) {
             while($sqlres = $todoqry->fetch_object()) {
               $todos[] = new todo($sqlres);
             }
          }
        }
      } else {
        $eventqry = mysql_query($eventsql);
        if((is_resource($eventqry)) && (mysql_num_rows($eventqry) > 0)) {
          while($sqlevent = mysql_fetch_object($eventqry)) {
            $events[] = new event($sqlevent);
          }
        }
        if(($opt_flags & OPT_TODO) != 0) {
          $todoqry = mysql_query($todosql);
          if((is_resource($todoqry)) && (mysql_num_rows($todoqry) > 0)) {
             while($sqlres = mysql_fetch_object($todoqry)) {
               $todos[] = new todo($sqlres);
             }
          }
        }
      }
    } elseif(IZ_CALMODE == 'postgre') {
      $eventqry = pg_query($eventsql);
      if((is_resource($eventqry)) && (pg_num_rows($eventqry) > 0)) {
        while($sqlevent = pg_fetch_object($eventqry)) {
          $events[] = new event($sqlevent);
        }
      }
      if(($opt_flags & OPT_TODO) != 0) {
        $todoqry = pg_query($todosql);
        if((is_resource($todoqry)) && (pg_num_rows($todoqry) > 0)) {
           while($sqlres = pg_fetch_object($todoqry)) {
             $todos[] = new todo($sqlres);
           }
        }
      }
    } elseif(IZ_CALMODE == 'msq') {
      $eventqry = mssql_query($eventsql);
      if((is_resource($eventqry)) && (mssql_num_rows($eventqry) > 0)) {
        while($sqlevent = mssql_fetch_object($eventqry)) {
          $events[] = new event($sqlevent);
        }
      }
      if(($opt_flags & OPT_TODO) != 0) {
        $todoqry = mssql_query($todosql);
        if((is_resource($todoqry)) && (mssql_num_rows($todoqry) > 0)) {
           while($sqlres = mssql_fetch_object($todoqry)) {
             $todos[] = new todo($sqlres);
           }
        }
      }
    }
    if(($_SESSION['canview'] != 1) || ($_SESSION['cpanel'] != 1)) {
      iz_error('#201: '.ERR_NOPERMISSION);
    } else {
      init_pdf();
      $pdf->SetFillcolor(204,223,235);
      $time = $_GET['time'];
      $row = array();
      $ridx = 0;
      if(!empty($events)) {
        $pdf->SetFont('','B',$stdfont+4);
        $pdf->Cell(0,10,STR_EVENTS,0,1,C,0);
        $pdf->SetFont('','',$stdfont);
        if($time == 'expanded') {
          $pdf->SetWidths(array(50,130));
          foreach($events as $event) {
            $pdf->SetFont('','B',$stdfont+2);
            $pdf->SetFillcolor(204,223,235);
            $pdf->Cell(180,$stdfont,dirty($event->title),1,1,L,1);
            $pdf->SetFont('','',$stdfont);
            if(($opt_flags & OPT_ENDDATE) != 0) {
              if($event->allday) {
//TODO: Add info 'i' image!!!
                $row[0]['filled'] = 'lblue';
                $row[0]['data'] = '<t2>'.STR_STARTS.'</t2>';
                $row[0]['align'] = 'R';
                $row[1]['filled'] = 'white';
                $row[1]['data'] = iz_longdate($event->eventstart).' ('.STR_ALLDAYDESC.')';
                $pdf->Row($row);
                $row = array();
              } else {
                $row[0]['filled'] = 'lblue';
                $row[0]['data'] = '<t2>'.STR_STARTS.'</t2>';
                $row[0]['align'] = 'R';
                $row[1]['filled'] = 'white';
                $row[1]['data'] = iz_longdatehours($event->eventstart);
                $pdf->Row($row);
                $row = array();

                $row[0]['filled'] = 'lblue';
                $row[0]['data'] = '<t2>'.STR_ENDS.'</t2>';
                $row[0]['align'] = 'R';
                $row[1]['filled'] = 'white';
                $row[1]['data'] = iz_longdatehours($event->eventend);
                $pdf->Row($row);
                $row = array();
              }
            } else {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.STR_DATE.'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = iz_longdatehours($event->eventstart);
              $pdf->Row($row);
              $row = array();
            }
            if(($opt_flags & OPT_VENUE) != 0) {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.STR_VENUE.'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($event->venue);
              $pdf->Row($row);
              $row = array();
            }                     
            if(($opt_flags & OPT_CATEGORY) != 0) {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.STR_CATEGORY.'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = $_SESSION['eventcats'][$event->category]->name;
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust1'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust1'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($event->cust1);
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust2'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust2'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($event->cust2);
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust3'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust3'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($event->cust3);
              $pdf->Row($row);
              $row = array();
            }
            $row[0]['filled'] = 'lblue';
            $row[0]['data'] = '<t2>'.STR_DESCRIPTION.'</t2>';
            $row[0]['align'] = 'R';
            $row[1]['filled'] = 'white';
            $row[1]['data'] = strip_tags(dirty($event->description));
            $pdf->Row($row);
            $row = array();
          
            $pdf->Ln();
          }
        } else {
          $colsize['name'] = 55;
          if(($opt_flags & OPT_ENDDATE) != 0) { $colsize['start'] = 25; $colsize['end'] = 25; $colsize['duration'] = 25; } else { $colsize['start'] = 25; }
          if(($opt_flags & OPT_VENUE) != 0) { $colsize['venue'] = 45; }
          if(($opt_flags & OPT_CATEGORY) != 0) { $colsize['cat'] = 45; }
          if($_SESSION['cust1'] != 'none') { $colsize['cust1'] = 40; }
          if($_SESSION['cust2'] != 'none') { $colsize['cust2'] = 40; }
          if($_SESSION['cust3'] != 'none') { $colsize['cust3'] = 40; }
          $pdf->SetWidths($colsize);
         
          $row['name']['data'] = '<t1>'.STR_NAME.'</t1>';
          if(($opt_flags & OPT_ENDDATE) != 0) {
            $row['start']['data'] = '<t1>'.STR_STARTS.'</t1>';
            $row['end']['data'] = '<t1>'.STR_ENDS.'</t1>';
            $row['duration']['data'] = '<t1>'.STR_DURATION.'</t1>';
          } else {
            $row['start']['data'] = '<t1>'.STR_DATE.'</t1>';
          }
          if(($opt_flags & OPT_VENUE) != 0) {
            $row['venue']['data'] = '<t1>'.STR_VENUE.'</t1>';
          }
          if(($opt_flags & OPT_CATEGORY) != 0) {
            $row['cat']['data'] = '<t1>'.STR_CATEGORY.'</t1>';
          }
          if($_SESSION['cust1'] != 'none') {
            $row['cust1']['data'] = '<t1>'.$_SESSION['cust1'].'</t1>';
          }
          if($_SESSION['cust2'] != 'none') {
            $row['cust2']['data'] = '<t1>'.$_SESSION['cust2'].'</t1>';
          }
          if($_SESSION['cust3'] != 'none') {
            $row['cust3']['data'] = '<t1>'.$_SESSION['cust3'].'</t1>';
          }
          $row['meta']['filled'] = 'lblue';
          $row['meta']['padding'] = 1;
          $pdf->Row_assoc($row);
          $row = array();

          $rc = 1;                  
          foreach($events as $event) {
            if(is_int($rc/2)) { 
              $row['meta']['filled'] = 'white';
            } else { 
              $row['meta']['filled'] = '207';
            }
            $rc++;
            $row['name']['data'] = dirty($event->title);
            if(($opt_flags & OPT_ENDDATE) != 0) {
              $row['start']['data'] = date($_SESSION['dateformat'],$event->eventstart);
              $row['end']['data'] = date($_SESSION['dateformat'],$event->eventend);
              if($event->allday) {
                   $row['duration']['data'] = STR_ALLDAYSHORT;
              } else {
                   $row['duration']['data'] = calcduration($event->eventstart,$event->eventend);
              }
            } else {
              $row['start']['data'] = date($_SESSION['dateformat'],$event->eventstart);
            }
            if(($opt_flags & OPT_VENUE) != 0) {
              $row['venue']['data'] = dirty($event->venue);
            }
            if(($opt_flags & OPT_CATEGORY) != 0) {
              $row['cat']['data'] = $_SESSION['eventcats'][$event->category]->name;
            }
            if($_SESSION['cust1'] != 'none') {
              $row['cust1']['data'] = dirty($event->cust1);
            }
            if($_SESSION['cust2'] != 'none') {
              $row['cust2']['data'] = dirty($event->cust2);
            }
            if($_SESSION['cust3'] != 'none') {
              $row['cust3']['data'] = dirty($event->cust3);
            }
            $pdf->Row_assoc($row);
            $row = array();
          }
        }
      }
      if(!empty($todos)) {
        if(!empty($events)) {
          $pdf->AddPage();
        }
        $pdf->SetFont('','B',$stdfont+4);
        $pdf->Cell(0,10,STR_TASKS,0,1,C,0);
        $pdf->SetFont('','',$stdfont);
        if($time == 'expanded') {
          $pdf->SetWidths(array(50,130));
          foreach($todos as $todo) {
            $pdf->SetFont('','B',$stdfont+2);
            $pdf->SetFillcolor(204,223,235);
            $pdf->Cell(180,$stdfont,dirty($todo->title),1,1,L,1);
            $pdf->SetFont('','',$stdfont);
//TODO: Add info 'i' image!!!
            $row[0]['filled'] = 'lblue';
            $row[0]['data'] = '<t2>'.STR_DATEDUE.'</t2>';
            $row[0]['align'] = 'R';
            $row[1]['filled'] = 'white';
            if($todo->due == 0) {
              $row[1]['data'] = STR_ONGOING;
            } else {
              if($todo->complete) {
                $row[1]['data'] = iz_longdate($todo->due).' ('.STR_COMPLETED.')';
              } else {
                $row[1]['data'] = iz_longdate($todo->due);
              }
            }
            $pdf->Row($row);
            $row = array();                  
            if(($opt_flags & OPT_CATEGORY) != 0) {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.STR_CATEGORY.'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = $_SESSION['todocats'][$todo->category]->name;
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust1'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust1'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($todo->cust1);
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust2'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust2'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($todo->cust2);
              $pdf->Row($row);
              $row = array();
            }
            if($_SESSION['cust3'] != 'none') {
              $row[0]['filled'] = 'lblue';
              $row[0]['data'] = '<t2>'.$_SESSION['cust3'].'</t2>';
              $row[0]['align'] = 'R';
              $row[1]['filled'] = 'white';
              $row[1]['data'] = dirty($todo->cust3);
              $pdf->Row($row);
              $row = array();
            }
            $row[0]['filled'] = 'lblue';
            $row[0]['data'] = '<t2>'.STR_DESCRIPTION.'</t2>';
            $row[0]['align'] = 'R';
            $row[1]['filled'] = 'white';
            $row[1]['data'] = dirty($todo->description);
            $pdf->Row($row);
            $row = array();
          
            $pdf->Ln();
          }
        } else {
          $colsize = array('name' => 50,
                           'desc' => 80,
                           'due' => 40
                          );
          if(($opt_flags & OPT_CATEGORY) != 0) { $colsize['cat'] = 45; }
          if($_SESSION['cust1'] != 'none') { $colsize['cust1'] = 30; }
          if($_SESSION['cust2'] != 'none') { $colsize['cust2'] = 30; }
          if($_SESSION['cust3'] != 'none') { $colsize['cust3'] = 30; }
          $pdf->SetWidths($colsize);
          $row['name']['data'] = '<t1>'.STR_TASKNAME.'</t1>';
          $row['desc']['data'] = '<t1>'.STR_DESCRIPTION.'</t1>';
          $row['due']['data'] = '<t1>'.STR_DATEDUE.'</t1>';
          if(($opt_flags & OPT_CATEGORY) != 0) {
            $row['cat']['data'] = '<t1>'.STR_CATEGORY.'</t1>';
          }
          if($_SESSION['cust1'] != 'none') {
            $row['cust1']['data'] = '<t1>'.$_SESSION['cust1'].'</t1>';
          }
          if($_SESSION['cust2'] != 'none') {
            $row['cust2']['data'] = '<t1>'.$_SESSION['cust2'].'</t1>';
          }
          if($_SESSION['cust3'] != 'none') {
            $row['cust3']['data'] = '<t1>'.$_SESSION['cust3'].'</t1>';
          }
          $row['meta']['filled'] = 'lblue';
          $row['meta']['padding'] = 1;
          $pdf->Row_assoc($row);
          $row = array();
          $rc = 1;                  
          foreach($todos as $todo) {
            if(is_int($rc/2)) { 
              $row['meta']['filled'] = 'white';
            } else { 
              $row['meta']['filled'] = '207';
            }
            $rc++;
            $row['name']['data'] = dirty($todo->title);
            if(strlen($todo->description) > 20) {
              $row['desc']['data'] = substr(nobreaks(dirty($todo->description)),0,20).'...';
            } else {
              $row['desc']['data'] = nobreaks($todo->description);
            }
            if($todo->due == 0) {
              $row['due']['data'] = STR_ONGOING;
            } else {
              if($todo->complete) {
                $row['due']['data'] = date($_SESSION['dateformat'],$todo->due).' ('.STR_COMPLETE.')';
              } else {
                $row['due']['data'] = date($_SESSION['dateformat'],$todo->due);
              }
            }
            if(($opt_flags & OPT_CATEGORY) != 0) {
              $row['cat']['data'] = $_SESSION['todocats'][$todo->category]->name;
            }
            if($_SESSION['cust1'] != 'none') {
              $row['cust1']['data'] = dirty($todo->cust1);
            }
            if($_SESSION['cust2'] != 'none') {
              $row['cust2']['data'] = dirty($todo->cust2);
            }
            if($_SESSION['cust3'] != 'none') {
              $row['cust3']['data'] = dirty($todo->cust3);
            }
            $pdf->Row_assoc($row);
            $row = array();
          }
        }
      }
      $pdf->Output();
    }
    break;
  case "event":
    izsql();
    $id = $_GET['time'];
    $sql = "SELECT * FROM events WHERE id='$id'";
    if(IZ_CALMODE == 'mysql') {
      if($_SESSION['MYSQLi']) {
        global $dblink;
        if($qry = $dblink->query($sql)) {
          $sqlevent = $qry->fetch_object();
          $event = new event($sqlevent);
        }
      } else {
        $qry = mysql_query($sql);
        $sqlevent = mysql_fetch_object($qry);
        if(is_resource($qry)) {
          $event = new event($sqlevent);
        }
      }
    } elseif(IZ_CALMODE == 'postgre') {
      $qry = pg_query($sql);
      $sqlevent = pg_fetch_object($qry);
      if(is_resource($qry)) {
        $event = new event($sqlevent);
      }
    } elseif(IZ_CALMODE == 'msq') {
      $qry = mssql_query($sql);
      $sqlevent = mssql_fetch_object($qry);
      if(is_resource($qry)) {
        $event = new event($sqlevent);
      }
    }
    if(empty($event->title)) {
      iz_error(ERR_EVNOTFOUND);
    } else if(($_SESSION['cpanel'] == 1) 
        || $event->status == 'Public'
        || ($event->status == 'Private'
            && (strtolower($event->author) == strtolower($_SESSION['uname'])
            || (strtolower($event->author) == 'everyone')
           )
        )) {
      init_pdf();
      $pdf->SetFont('','B');
      $pdftitle = 'iZeit - '.$event->title.'.pdf';
    
      $pdf->SetWidths(array(80,200));
      $pdf->Cell(0,10,STR_VIEWINGEVENT,0,1,L,0);
      $pdf->SetFont('','',$stdfont);
      $ridx = 0;
      $row = array();
      
      $row[$ridx]['filled'] = 'lblue';
      $row[$ridx]['data'] = '<t1>'.STR_EVENTNAME.'</t1>';
      $row[$ridx]['align'] = 'R';
      $row[$ridx]['padding'] = 1;
      $celltxt = '';
      $ridx++;
      $row[$ridx]['filled'] = 'white';
      $row[$ridx]['data'] = dirty($event->title);
      $row[$ridx]['padding'] = 1;
      $celltxt = '';
      $ridx++;

      $pdf->Row($row);
      $row = array();
      $ridx = 0;
      
      if((!empty($event->venue)) && (($opt_flags & OPT_VENUE) != 0)) {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.STR_VENUE.'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
      
        $row[$ridx]['filled'] = 'white';
        if(($event->linkgoogle) && ($opt_flags & OPT_GOOGLE) != 0) {
          $row[$ridx]['data'] = '<a href="http://maps.google.co.uk/maps?q='.$event->venue.'" title="'.STR_LINKGOOGLE.'">'.dirty($event->venue).'</a>';	  
        } else {
          $row[$ridx]['data'] = dirty($event->venue);
        }
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
         $pdf->Row($row);
        $row = array();
        $ridx = 0;
      } 
      if(($opt_flags & OPT_CATEGORY) != 0) {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.STR_CATEGORY.'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = $_SESSION['eventcats'][$event->category]->name;
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      } 
      if(($opt_flags & OPT_ENDDATE) != 0) {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.STR_STARTS.'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        if(intval($event->allday)) {
          $row[$ridx]['data'] = iz_longdate($event->eventstart).' ('.STR_ALLDAYDESC.')';
        } else {
          $row[$ridx]['data'] = iz_longdatehours($event->eventstart);
        }
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.STR_ENDS.'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        if(intval($event->allday)) {
          $row[$ridx]['data'] = iz_longdate($event->eventend);
        } else {
          $row[$ridx]['data'] = iz_longdatehours($event->eventend);
        }
        
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      } else {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.STR_DATE.'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = iz_longdate($event->eventstart);
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;

        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      if(!empty($event->description)) {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.STR_DESCRIPTION.'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = strip_tags(dirty($event->description));
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      if((!empty($event->attendees)) && (($opt_flags & OPT_ATTENDEES) != 0)) {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.STR_ATTENDEES.'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = $event->attendees;
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      if($_SESSION['cust1'] != 'none') {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.$_SESSION['cust1'].'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = $event->cust1;
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      if($_SESSION['cust2'] != 'none') {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.$_SESSION['cust2'].'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = $event->cust2;
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      if($_SESSION['cust3'] != 'none') {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.$_SESSION['cust3'].'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = $event->cust3;
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      if($_SESSION['cpanel']) {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.STR_AUTHOR.'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        if(strtolower($event->author) == 'everyone') {
          $row[$ridx]['data'] = STR_NOTLOGGEDIN;
        } else {
          $row[$ridx]['data'] = $event->author;
        }
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      $pdf->Output($pdftitle,'I');
    } else {
      iz_error('#202: '.ERR_NOPERMISSION);
    }
    break;
  case "todo":
    izsql();
    $id = $_GET['time'];
    $sql = "SELECT * FROM tasks WHERE id='$id'";
    if(IZ_CALMODE == 'mysql') {
      if($_SESSION['MYSQLi']) {
        global $dblink;
        if($qry = $dblink->query($sql)) {
          $sqltodo = $qry->fetch_object($qry);
          $todo = new todo($sqltodo);
        }
      } else {
        $qry = mysql_query($sql);
        $sqltodo = mysql_fetch_object($qry);
        if(is_resource($qry)) {
          $todo = new todo($sqltodo);
        }  
      }
    } elseif(IZ_CALMODE == 'postgre') {
      $qry = pg_query($sql);
      $sqltodo = pg_fetch_object($qry);
      if(is_resource($qry)) {
        $todo = new todo($sqltodo);
      }
    } elseif(IZ_CALMODE == 'msq') {
      $qry = mssql_query($sql);
      $sqltodo = mssql_fetch_object($qry);
      if(is_resource($qry)) {
        $todo = new todo($sqltodo);
      }
    }
    if(empty($todo->id)) {
      iz_error(ERR_EVNOTFOUND);
    } elseif(($_SESSION['cpanel'] == 1) 
        || $todo->status == 'Public'
        || ($todo->status == 'Private'
            && (strtolower($todo->author) == strtolower($_SESSION['uname'])
            || (strtolower($todo->author) == 'everyone')
           )
        )) {
      init_pdf();
      $pdftitle = 'iZeit - '.$todo->title.'.pdf';
      $pdf->SetFillcolor(204,223,235);
      $pdf->SetFont('','B');
      $end = $time+86399;
      $events = array();
      $row = array();
      $ridx = 0;
      $pdf->SetWidths(array(60,215));
      $pdf->Cell(0,10,STR_VIEWINGTASK,0,1,L,0);
      $pdf->SetFont('','',$stdfont);
    
      $row[$ridx]['filled'] = 'lblue';
      $row[$ridx]['data'] = '<t1>'.STR_TASKNAME.'</t1>';
      $row[$ridx]['align'] = 'R';
      $row[$ridx]['padding'] = 1;
      $celltxt = '';
      $ridx++;
      $row[$ridx]['filled'] = 'white';
      $row[$ridx]['data'] = dirty($todo->title);
      $row[$ridx]['padding'] = 1;
      $celltxt = '';
      $ridx++;

      $pdf->Row($row);
      $row = array();
      $ridx = 0;
      
      $row[$ridx]['filled'] = 'lblue';
      $row[$ridx]['data'] = '<t1>'.STR_DATEDUE.'</t1>';
      $row[$ridx]['align'] = 'R';
      $row[$ridx]['padding'] = 1;
      $celltxt = '';
      $ridx++;
      $row[$ridx]['filled'] = 'white';
      if($todo->due == 0) {
        $row[$ridx]['data'] = STR_ONGOING;
      } else {
        $row[$ridx]['data'] = iz_longdate($todo->due);
      }
      if($todo->complete) {
        $row[$ridx]['data'] .= ' ('.STR_COMPLETE.')';
      }
      $row[$ridx]['padding'] = 1;
      $celltxt = '';
      $ridx++;

      $pdf->Row($row);
      $row = array();
      $ridx = 0;

      $row[$ridx]['filled'] = 'lblue';
      $row[$ridx]['data'] = '<t1>'.STR_CATEGORY.'</t1>';
      $row[$ridx]['align'] = 'R';
      $row[$ridx]['padding'] = 1;
      $celltxt = '';
      $ridx++;
      $row[$ridx]['filled'] = 'white';
      $row[$ridx]['data'] = $_SESSION['todocats'][$todo->category]->name;
      $row[$ridx]['padding'] = 1;
      $celltxt = '';
      $ridx++;

      $pdf->Row($row);
      $row = array();
      $ridx = 0;
      
      if($_SESSION['cust1'] != 'none') {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.$_SESSION['cust1'].'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = dirty($todo->cust1);
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;

        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      if($_SESSION['cust2'] != 'none') {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.$_SESSION['cust2'].'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = dirty($todo->cust2);
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;

        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      if($_SESSION['cust3'] != 'none') {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.$_SESSION['cust3'].'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = dirty($todo->cust3);
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;

        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      if(!empty($todo->description)) {
        $row[$ridx]['filled'] = 'lblue';
        $row[$ridx]['data'] = '<t1>'.STR_TASK.'</t1>';
        $row[$ridx]['align'] = 'R';
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;
        $row[$ridx]['filled'] = 'white';
        $row[$ridx]['data'] = dirty($todo->description);
        $row[$ridx]['padding'] = 1;
        $celltxt = '';
        $ridx++;

        $pdf->Row($row);
        $row = array();
        $ridx = 0;
      }
      $pdf->Output($pdftitle,'I');
    } else {
      iz_error('#203: '.ERR_NOPERMISSION);
    }
    break;
}
} else {
  // Directly accessed
  header('Location: ../');
} ?>